package operation;

import book.Book;
import book.Booklist;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wx
 * Date: 2023-11-14
 * Time: 14:49
 */
public class DelOperation implements IOPeration{




        @Override
    public void work(Booklist booklist) {
            System.out.println("*******  删除图书:  *******");

            System.out.println("**** 请输入要删除的图书: ****");
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();

            int pos = -1;
            int currentSize = booklist.getUsedsize();
            for (int i = 0; i < currentSize; i++) {
                Book book = booklist.getbook(i);
                if (book.getName().equals(name)) {
                    pos = i;
                    break;
                }
                if (i == currentSize) {
                    System.out.println("*******  没有这本书:  ******");
                    return;
                }
            }

            //开始删除
            int j = pos;
            for (; j < currentSize-1; j++) {
                Book book = booklist.getbook(j+1);

                booklist.setbook(j,book);
            }
            booklist.setbook(j,null);

            booklist.setUsedsize(currentSize - 1);
        }

    }

